﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace WhatCatch.Utilities
{
    public class HttpHelper
    {
        /// <summary>
        /// URL
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static HttpWebResponse GetResponse(string url)
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            myHttpWebRequest.AllowAutoRedirect = false;//不允许重定向
            myHttpWebRequest.Timeout = 10 * 1000;//超时10秒
            myHttpWebRequest.Method = "GET";
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
            myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215;)";
            myHttpWebRequest.ContentType = "text/html";
            myHttpWebRequest.KeepAlive = true;
            myHttpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
            myHttpWebRequest.ServicePoint.ConnectionLimit = 300;
            myHttpWebRequest.UseDefaultCredentials = true;

            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            return myHttpWebResponse;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetHtml(string url)
        {
            var response = GetResponse(url);
            if (response == null)
            {
                return "";
            }
            Stream stream = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(stream, Encoding.UTF8);
            string htmlString = streamReader.ReadToEnd();
            response.Close();
            return htmlString;
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="url"></param>
        /// <param name="path"></param>
        public static void DownLoadFile(string url, string filePath, int timeOut = 10)
        {
            WebRequest request = WebRequest.Create(url);
            request.Timeout = timeOut * 1000;//超时
                                             //request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 /29.0";//Gecko浏览器

            WebResponse response = request.GetResponse();

            Stream reader = response.GetResponseStream();
            FileStream writer = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
            byte[] buff = new byte[512];
            int c = 0; //实际读取的字节数
            while ((c = reader.Read(buff, 0, buff.Length)) > 0)
            {
                writer.Write(buff, 0, c);
            }
            writer.Close();
            writer.Dispose();
            reader.Close();
            reader.Dispose();
            response.Close();
        }
    }
}
